<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:m="livcos.org/ns/map"
  xmlns:u="livcos.org/ns/cosmos/update"
  exclude-result-prefixes="m u doc">
  
<xsl:import href="../../web/editor/integrity.xsl"/>

	<xsl:template match="m:map/m:body/*[@id]">
		<xsl:variable name="itemId">
			<xsl:value-of select="preceding-sibling::m:item[0]/@id"/>
			<xsl:if test="not(preceding-sibling::m:item[0]/@id)">
				<xsl:value-of select="following-sibling::m:item[0]/@id"/>
			</xsl:if>
		</xsl:variable>
		<u:insert select=".//*[@id = '{$itemId}']">
			<u:move-from select="../*[@id = '{@id}']"/>
		</u:insert>
	</xsl:template>
	
	<xsl:template match="m:map/m:body/m:item" priority="2">
		<xsl:apply-templates select="*"/>
	</xsl:template>
	
	<xsl:template match="m:item/m:rels/*[@id]">
		<u:insert select=".//*[@id = '{../../@id}']">
			<u:move-from select="*/*[@id = '{@id}']"/>
		</u:insert>
	</xsl:template>
	
	<xsl:template match="m:item/m:rels/m:rel" priority="2">
		<xsl:apply-templates select="*"/>
	</xsl:template>
	
</xsl:stylesheet>
